package chaw.android.train;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
import chaw.android.train.R;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;

public class FacebookActivity extends Activity{
	private WebView facebookWebView;
	private ImageView btnBack;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facebook_login);
		
		facebookWebView = (WebView) findViewById(R.id.facebookWebView);
		String url = ServerURL.facebookLogin;
		facebookWebView.loadUrl(url);

		facebookWebView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url){
			    view.loadUrl(url);
				if(url.startsWith("http://traintravelers.co.kr/")){
				    view.loadUrl(url);
				    if(url.startsWith("http://traintravelers.co.kr/web/app/access_token")){
				    	String[] access = url.split("access_token=");
				    	String[] token = access[1].split("#");
				    	Preferences.setValue(getBaseContext(), "access_token", token[0]);
				    	Log.d("d", "url : " + url + "   ||||   access_token : " + token[0]);
				    	Toast.makeText(getBaseContext(), "등록이 완료되었습니다.", Toast.LENGTH_SHORT).show();
						finish();
				    }
				}
				return true;
			}
		});
		facebookWebView.getSettings().setJavaScriptEnabled(true);
		btnBack = (ImageView) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onBackPressed() {
		finish();
	}
}
